package edu.luc.cs413s13.team1.monsterm.model.timer;

import java.util.Timer;
import java.util.TimerTask;

import edu.luc.cs413s13.team1.monsterm.model.TimerChangeListener;

import android.os.Handler;

/**
 * Timer that ticks every 20-seconds to notify level transitions
 *
 */
public class LevelTimer implements MonstermTimer {

	private Timer timer;
	
	@Override
	public void setOnTimerChangeListener(TimerChangeListener listener) {
		this.timerChangeListener = listener;
	}
	private TimerChangeListener timerChangeListener;
	
	private Handler handler = new Handler();
	
	@Override
	public void start() {
		timer = new Timer();

		// The tickModel runs performAction() every 2000 milliseconds
		timer.schedule(new TimerTask() {
			@Override public void run() {
				// schedule the onTick event on the UI thread
				handler.post(new Runnable() {
					@Override public void run() {
						// fire event
						timerChangeListener.onTimerChange(LevelTimer.this);
					}
				});
			}
		}, /* Fire after */ 20000);
	}

	@Override
	public void stop() {
		timer.cancel();
	}

}
